package com.example.crm01.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.crm01.common.Result;
import com.example.crm01.entity.Client;
import com.example.crm01.page.TableDataInfo;
import com.example.crm01.service.ClientService;
import com.example.crm01.service.form.client.ClientAddForm;
import com.example.crm01.service.form.BaseIdForm;
import com.example.crm01.service.form.client.ClientListForm;
import com.example.crm01.service.vo.client.ClientMonthSumVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@Api(tags = "客户功能")
@RestController
@RequestMapping("/client")
public class ClientController {


    @Autowired
    private ClientService clientService;

    @ApiOperation("当月客户数")
    @PostMapping(value = "/searchClientMonth")
    public Result<ClientMonthSumVO> searchClientMonth() {
        return Result.success(clientService.searchClientMonth());
    }


    @ApiOperation("客户列表")
    @PostMapping(value = "/list")
    public TableDataInfo list(@RequestBody ClientListForm form) {
        TableDataInfo info = new TableDataInfo();
        log.info("打印入参的参数，form：{}", form);
        Page<Client> clientPageInfo = clientService.selectClientList(form);
        info.setCode(200);
        info.setMsg("请求成功");
        info.setRows(clientPageInfo.getRecords());
        info.setTotal(clientPageInfo.getTotal());
        return info;
    }

    @ApiOperation("客户新增")
    @PostMapping(value = "/add")
    public Result<Boolean> add(@RequestBody @Validated ClientAddForm form) {
        return Result.success(clientService.insertClient(form));
    }

    @ApiOperation("客户修改")
    @PostMapping(value = "/update")
    public Result<Boolean> update(@RequestBody @Validated ClientAddForm form) {
        return Result.success(clientService.updateClient(form));
    }


    @ApiOperation("客户删除")
    @PostMapping(value = "/delete")
    public Result<Boolean> delete(@RequestBody @Validated BaseIdForm form) {
        return Result.success(clientService.deleteClient(form));
    }

    @ApiOperation("客户详情")
    @PostMapping(value = "/details")
    public Result<Client> details(@RequestBody @Validated BaseIdForm form) {
        return Result.success(clientService.detailsClient(form));
    }

}
